home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / amiexpress / source / doors / multichatdoor / sendmsg.c < prev   
Encoding:
C/C++ Source or Header  |  1995-09-05  |  1.7 KB  |  75 lines

  1. #include <exec/types.h>
  2. #include <exec/ports.h>
  3. #include <exec/memory.h>
  4. #include <dos/dos.h>
  5. #include <clib/exec_protos.h>
  6. #include <clib/alib_protos.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "MultiChat.h"
  10. #include <doorheader.h>
  11. #include "ChatMsg.h"
  12.  
  13. int SafePutToPort(struct Message *, STRPTR);
  14.  
  15. UWORD SendMsg(char Text)
  16. {
  17.     struct ChatMsgStruct *ChatMsg;
  18.     struct MsgPort *ChatMsgReplyPort;
  19.  
  20.     extern char *PortNames[];
  21.     extern char *ReplyPortNames[];
  22.     char        Buf[100];
  23.     char        YourNode_char[200];
  24.     
  25.     UWORD    NodeNr;
  26.     UWORD    Status = TRUE;
  27.     UWORD    YourNode;
  28.  
  29.  
  30.     getuserstring(YourNode_char, BB_NODEID);
  31.     YourNode = atoi(YourNode_char);
  32.  
  33.     for (NodeNr = 0; NodeNr <= 32; NodeNr++)
  34.     {
  35.         if (YourNode != NodeNr)
  36.         {
  37.             if (ChatMsg = (struct ChatMsgStruct *)AllocMem(sizeof(struct ChatMsgStruct), MEMF_PUBLIC | MEMF_CLEAR))
  38.             {
  39.                 if (ChatMsgReplyPort = CreatePort(ReplyPortNames[NodeNr], 0))
  40.                 {
  41.                     ChatMsg->chat_Msg.mn_Node.ln_Type = NT_MESSAGE;
  42.                     ChatMsg->chat_Msg.mn_Length = sizeof(struct ChatMsgStruct);
  43.                     ChatMsg->chat_Msg.mn_ReplyPort = ChatMsgReplyPort;
  44.                     ChatMsg->Text = Text;
  45.                  
  46.                     if (SafePutToPort((struct Message *)ChatMsg, PortNames[NodeNr]))
  47.                     {
  48.                         WaitPort(ChatMsgReplyPort);
  49.                         ChatMsg = (struct ChatMsgStruct *)GetMsg(ChatMsgReplyPort);
  50.                     }
  51.                     DeletePort(ChatMsgReplyPort);
  52.                 }
  53.                 FreeMem(ChatMsg, sizeof(struct ChatMsgStruct));
  54.             }
  55.             else
  56.             {
  57.                 sprintf(Buf, "Couldn't allocate memory for message structure.\r\n");
  58.                 sendmessage(Buf, 0);
  59.             }
  60.         }
  61.     }
  62.     return(Status);
  63. }
  64.  
  65. int SafePutToPort( struct Message *message, STRPTR portname)
  66. {
  67.     struct MsgPort *ChatPort;
  68.  
  69.     Forbid();
  70.     ChatPort = FindPort(portname);
  71.     if (ChatPort) PutMsg(ChatPort, message);
  72.     Permit();
  73.     return(ChatPort ? TRUE : FALSE);
  74. }
  75.